package com.mproject.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Locale;

import com.mproject.connection.DatabaseConnection;
import com.mproject.message.MessageUtil;
import com.mproject.message.ServerConfiguration;



public class WebServiceMethods {

	public static Connection con=DatabaseConnection.getConnection();


	//update user info in user table: INSERT if user does not exist in the table
	public static boolean UpdateUserInfo(String userid,
			String name,String regid)
	{
		try {
			Statement stmt=con.createStatement();

			//check this userid in database or not
			String query="select count(*) from user where nusnet=\""+userid+"\";";
			System.out.println(query);
			ResultSet rs=stmt.executeQuery(query);
			int numRow = 0;
			while (rs.next())
				numRow = rs.getInt(1);
			System.out.println("Number of row user" +numRow+"");

			if (numRow == 0) {
				//if does not exist in database -> insert
				query = "insert into user (nusnet,name,mac) values('"+userid+"','"+name+"','"+regid+"');";
				System.out.println(query);
			}
			else {
				//already in database -> update
				query = "update user set mac='"+regid+"' where nusnet='"+userid+"';";
				System.out.println(query);
			}

			//execute sql
			stmt.executeUpdate(query);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;

		}

	}

	//send message method
	public static String sendMessage(String from_user, String to_user, String message, String project_id, String from_name, String project_name)
	{
		String rsep="";
		String orig_msg=message;
		System.out.println(message);
		
		try
		{

			String from_id=null,to_id=null;
			Statement stmt=con.createStatement();
			String name="";
			String query="select mac from user where nusnet='"+to_user+"'";
			ResultSet rs=stmt.executeQuery(query);
			while(rs.next())
			{
				to_id=rs.getString("mac");
				//name=rs.getString("name");
			}
			/*query="select name from user where nusnet='"+from_user+"'";
			rs=stmt.executeQuery(query);
			while(rs.next())
			{
				name=rs.getString("name");
			}
			System.out.println(to_user+" "+to_id);*/
			if(to_id==null || to_id.equalsIgnoreCase("null"))
			{
				return "\"Error::User not yet registered\"}";
			}

			//String msg="{\"#error\": false,\"from\":\""+from_user+"\",\"name\":\""+name+"\",\"to\":\""+to_user+"\",\"data\":";
			orig_msg=orig_msg.replace("\'", "%xy");
			orig_msg=orig_msg.replace("\"", "%yz");
			orig_msg=orig_msg.replace("\\", "%xz");
			
			String send_message = "{\"message\":{\"project_id\":\""+project_id+"\",\"project_name\":\""+
									project_name+"\",\"sender_id\":\""+from_user+"\",\"sender_name\":\""+from_name+
									"\",\"msg\":\""+orig_msg+"\"}}";

			/*int responescode=MessageUtil.sendMessage(
					ServerConfiguration.AUTHENTICATION_TOKEN,
					to_id, msg+"\""+orig_msg+"\"}");*/

			int responescode=MessageUtil.sendMessage(
					ServerConfiguration.AUTHENTICATION_TOKEN,to_id,
					send_message);


			if(responescode==200)
			{
				Calendar calendar = Calendar.getInstance(Locale.getDefault());
				long timestamp = calendar.getTimeInMillis();
				query="insert into message(from_id,to_id,project_id,message,date) values('"+from_user+"','"+to_user+"','"+project_id+"','"+message+ "','"+timestamp+   "')";
				int i;
				i=stmt.executeUpdate(query);

				if(i<=0)
					return "\"error\"}";
				else
					return "\"MessageSent\"}";
			}
			else
			{
				return "\"cannot send\"}";
			}

		}
		catch(Exception e)
		{
			e.printStackTrace();

		}

		return "\"error\"}";
	}

}
